﻿using System;
using System.Diagnostics.CodeAnalysis;
using System.Net.Http;
using System.Threading.Tasks;
using IdentityModel.Client;
using Newtonsoft.Json.Linq;

namespace IDSDemo.ConsoleApp
{
    class Program
    {
        static async Task Main(string[] args)
        {
            var client = new HttpClient();
            var disc = await client.GetDiscoveryDocumentAsync("https://localhost:5000");
            if (disc.IsError)
            {
                Console.WriteLine(disc.Error);
                return;
            }

            var tokenResponse = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest()
            {
                Address = disc.TokenEndpoint,
                ClientId ="sample_api",
                ClientSecret = "sample_client_secret"

            });

            if (tokenResponse.IsError)
            {
                Console.WriteLine(tokenResponse.Error);
                return;
            }

            Console.WriteLine(tokenResponse.AccessToken);
            var apiClient = new HttpClient();
            apiClient.SetBearerToken(tokenResponse.AccessToken);
            var response = await apiClient.GetAsync("https://localhost:5002/api/resource");
            var content = await response.Content.ReadAsStringAsync();


           Console.WriteLine($"response {JArray.Parse(content)}");
           Console.ReadLine();
        }
    }
}
